package com.qst.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qst.survey.entity.Type;
import com.qst.survey.mapper.QuestionMapper;
import com.qst.survey.mapper.TypeMapper;
import com.qst.survey.service.TypeService;
import com.qst.survey.util.ColorUtil;
import com.qst.survey.vo.BarVO;
import com.qst.survey.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date: 2021/3/21 14:21
 * @Description: 类型具体服务类
 */

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    TypeMapper typeMapper;
    @Autowired
    QuestionMapper questionMapper;
    /**
     * @description  TODO 查出所以类型数据
     * @return java.util.List<com.qst.survey.entity.Type>
     * @date 2021/4/2
     */
    @Override
    @Transactional
    public List<Type> findAll() {
        return  typeMapper.selectList(null);
    }
    /**
     * @description  TODO 查出柱状图的数据
     * @return com.qst.survey.vo.BarVO
     * @date 2021/4/2
     */
    @Override
    public BarVO getBar() {
        List<Type> types = typeMapper.selectList(null);
        BarVO barVO=new BarVO();
        List<String>names=new ArrayList<>();
        List<DataVO>dataList=new ArrayList<>();
        for (Type type : types) {
            names.add(type.getMsg());
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("type",type.getId());
            int count=questionMapper.selectCount(queryWrapper);
            Map<String, String> itemStyle = ColorUtil.createItemStyle(count);
            DataVO dataVO=new DataVO();
            dataVO.setValue(count);
            dataVO.setItemStyle(itemStyle);
            dataList.add(dataVO);
        }
        barVO.setXData(names);
        barVO.setYData(dataList);
        return barVO;
    }
}
